package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.common.response.JsonResult;
import cn.tedu._05mvcboot02.common.response.StatusCode;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.AddUserDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "用户模块")
@Slf4j
//@Controller
@RestController
@RequestMapping(value = "v1/users/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @ApiOperation(value = "添加用户功能")
    @PostMapping(value = "insert")
    public JsonResult addUser(AddUserDTO addUserDTO){
        log.debug("addUserDTO ="+ addUserDTO);
        User user = new User();
        BeanUtils.copyProperties(addUserDTO,user);
        user.setCreated(new Date());
        userMapper.insertUser(user);
        log.debug("user = "+user);
        return JsonResult.ok();
    }

    @ApiOperation(value = "获取用户列表功能")
    @GetMapping(value = "userList")
    public JsonResult UserList(){
        List<UserListVO> userListVOS = userMapper.selectUserList();
        return JsonResult.ok(userListVOS);
    }

    @ApiOperation(value = "删除用户功能")
    @GetMapping(value = "delete")
    @ApiImplicitParam(name = "id",value = "用户ID" ,dataType = "int")
    public JsonResult deleteUser( int id){
        if (id<10){
            throw new IllegalArgumentException("id不能小于0");
        }
        userMapper.deleteUser(id);
        return JsonResult.ok();
    }

    @ApiOperation(value = "修改用户功能")
    @PostMapping(value = "update")
    public JsonResult updateUser(User user){
        log.debug("user = "+user);
        userMapper.updateUser(user);
        return JsonResult.ok();
    }

    @ApiOperation(value = "查询用户功能")
    @ApiImplicitParam(name = "id",value = "用户ID" ,dataType = "int")
    @GetMapping(value = "{id}")
    public JsonResult getUserById(@PathVariable int id){
        User user = userMapper.selectUserById(id);
        return JsonResult.ok(user);
    }


}
